package com.avic.rong.course;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.avic.rong.BaseFragment;
import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.adapter.WebBannerAdapter;
import com.avic.rong.bean.AdvBean;
import com.avic.rong.bean.show_bean.CultivateBean;
import com.avic.rong.course.activity.PeiXunBookShelfActivity;
import com.avic.rong.course.activity.PeiXunFindExpertActivity;
import com.avic.rong.course.activity.PeiXunLineClassActivity;
import com.avic.rong.course.activity.PeiXunVideoActivity;
import com.avic.rong.course.adapter.CourseIndexAdapter;
import com.avic.rong.course.showapi.CultivateApi;
import com.avic.rong.listener.RocketOnScrollListener;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.wedgit.StringUtil;
import com.example.library.banner.BannerLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 * Created by XinEr on 2019/8/10.
 * Describe:
 */
public class FragmentCultivate extends BaseFragment implements View.OnClickListener, OnRefreshListener {


    private Context mContext;
    private String mTitle;

    //猜你需要
//    private Cultivate_Two_RecvAdapter cultivate_two_recvAdapter;
    //精品推荐
//    private CultivateRecvAdapter cultivateRecvAdapter;
    //猜你需要

    //精品推荐

    private List<CultivateBean.BannerBean> bannerBeanList = new ArrayList<>();
    private CourseIndexAdapter courseIndexAdapter;
    ArrayList<CultivateBean.CourseBean> dataList = new ArrayList<>();
    private CultivateBean data;
    private View view;
    private BannerLayout recycler_banner;
    /**
     * 视频课
     */
    private TextView tv_video;
    /**
     * 线下课
     */
    private TextView tv_offline;
    /**
     * 找专家
     */
    private TextView tv_findexpert;
    /**
     * 书架
     */
    private TextView tv_bookrack;
    private TextView tv_title;
    /**
     * 查看
     */
    private TextView paixun_chakan;
    private RecyclerView recv_cultivate;
    public SmartRefreshLayout courseRefresh;
    public NestedScrollView nestedScrollView;

    public boolean isRefresh = false;

    public static FragmentCultivate getInstance(String title, Context context) {
        FragmentCultivate sf = new FragmentCultivate();
        sf.mContext = context;
        sf.mTitle = title;
        return sf;
    }

    private void initData() {

        CultivateApi cultivateApi = new CultivateApi((MainActivity) getActivity(), httpOnNextListener, SharedPreferencesUtil.getLoginToken());
        cultivateApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(cultivateApi);
    }

    HttpOnNextListener<BaseResultEntity<CultivateBean>> httpOnNextListener = new HttpOnNextListener<BaseResultEntity<CultivateBean>>() {
        @Override
        public void onNext(BaseResultEntity<CultivateBean> baseResultEntity) {
            String code = baseResultEntity.getCode();
            if (isRefresh) {
                dataList.clear();
                courseRefresh.finishRefresh();
                isRefresh = false;
            }
            if (code.equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                data = baseResultEntity.getResult();
                String title = data.getNews().getTitle();
                if (!StringUtil.isEmpty(title)) {
                    tv_title.setText(title);
                }
                bannerBeanList.clear();
                bannerBeanList.addAll(data.getBanner());
                List<AdvBean> advBeanList = new ArrayList<>();
                AdvBean advBean = null;
                for (CultivateBean.BannerBean bannerBean : bannerBeanList) {
                    advBean = new AdvBean();
                    advBean.setContent(bannerBean.getContent());
                    advBean.setId(bannerBean.getId());
                    advBeanList.add(advBean);
                }

                WebBannerAdapter webBannerAdapter = new WebBannerAdapter(getContext(), advBeanList);
                recycler_banner.setAdapter(webBannerAdapter);
                recycler_banner.setAutoPlaying(true);
                recycler_banner.setAutoPlayDuration(5000);
               /* webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Logger.i("------------" + "点击了第  " + position + "  项");
                        String bannerUrl = AppUtils.COURSE_IMAGE_URL + bannerBeanList.get(position).getUrl();
                        SkipUtils.startSkipDetailIntent(mContext, bannerUrl, bannerBeanList.get(position).getUrl());
                    }
                });*/


                List<CultivateBean.CourseBean> coursesRecommend = data.getCoursesRecommend();//猜你需要
                List<CultivateBean.CourseBean> coursesSuperior = data.getCoursesSuperior();//精品



                CultivateBean.CourseBean superiorBean = new CultivateBean.CourseBean();
                superiorBean.setViewType(1);
                superiorBean.setViewName("精品推荐");
                dataList.add(superiorBean);
                dataList.addAll(coursesSuperior);


                CultivateBean.CourseBean recommendBean = new CultivateBean.CourseBean();
                recommendBean.setViewType(1);
                recommendBean.setViewName("猜你需要");
                dataList.add(recommendBean);
                dataList.addAll(coursesRecommend);


                courseIndexAdapter.setNoLinePosition(coursesSuperior.size() + 1);
                courseIndexAdapter.notifyDataSetChanged();
//                recommendBeans.addAll(coursesRecommend);
//                superiorBeans.addAll(coursesSuperior);

//                cultivateRecvAdapter.notifyDataSetChanged();
//                cultivate_two_recvAdapter.notifyDataSetChanged();
            }
        }
    };

 /*   @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_cultivate, null);
        unbinder = ButterKnife.bind(this, v);

        return v;
    }*/


   /* @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }*/

    @Override
    protected void initView() {
        courseRefresh = fragmentView.findViewById(R.id.course_refresh);
        recycler_banner = (BannerLayout) fragmentView.findViewById(R.id.recycler_banner);
        tv_video = (TextView) fragmentView.findViewById(R.id.tv_video);
        tv_offline = (TextView) fragmentView.findViewById(R.id.tv_offline);
        tv_findexpert = (TextView) fragmentView.findViewById(R.id.tv_findexpert);
        tv_bookrack = (TextView) fragmentView.findViewById(R.id.tv_bookrack);
        tv_title = (TextView) fragmentView.findViewById(R.id.tv_news_title);
        paixun_chakan = (TextView) fragmentView.findViewById(R.id.paixun_chakan);
        recv_cultivate = fragmentView.findViewById(R.id.recv_cultivate);
        nestedScrollView = (NestedScrollView)fragmentView.findViewById(R.id.nest_scrollview);
        courseRefresh.setOnRefreshListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        courseIndexAdapter = new CourseIndexAdapter(getContext(),dataList);
        courseRefresh.setEnableLoadMore(false);//是否启用上拉加载功能
        if (recv_cultivate != null) {
            recv_cultivate.setNestedScrollingEnabled(false);
            recv_cultivate.setLayoutManager(linearLayoutManager);
            recv_cultivate.setAdapter(courseIndexAdapter);
        }

        //监听滚动
        nestedScrollView.setOnScrollChangeListener(new RocketOnScrollListener(((MainActivity)getActivity())));

        initData();
    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_cultivate;
    }

    @OnClick({R.id.tv_video, R.id.tv_offline, R.id.tv_findexpert, R.id.tv_bookrack, R.id.paixun_chakan})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_video:
                mContext.startActivity(PeiXunVideoActivity.getIntent(mContext, PeiXunVideoActivity.class));
                break;
            case R.id.tv_offline:
                mContext.startActivity(PeiXunLineClassActivity.getIntent(mContext, PeiXunLineClassActivity.class));
                break;
            case R.id.tv_findexpert:
                mContext.startActivity(PeiXunFindExpertActivity.getIntent(mContext, PeiXunFindExpertActivity.class));

                break;
            case R.id.tv_bookrack:
                mContext.startActivity(PeiXunBookShelfActivity.getIntent(mContext, PeiXunBookShelfActivity.class));
                break;
            case R.id.paixun_chakan:
                Intent intent4 = new Intent(getContext(), ArticleDetailActivity.class);
                intent4.putExtra("contId", data.getNews().getContId());
                startActivity(intent4);
                break;
        }
    }



    /*public void loadData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        courseIndexAdapter = new CourseIndexAdapter(getContext());
        if (recv_cultivate != null) {
            recv_cultivate.setNestedScrollingEnabled(false);
            recv_cultivate.setLayoutManager(linearLayoutManager);
            recv_cultivate.setAdapter(courseIndexAdapter);
        }
        initData();
    }*/


  /*  @Override
    public void stopData() {
        super.stopData();

    }*/

    @Override
    public void onStop() {
        super.onStop();
//        if (banner != null) {
//            banner.stopAutoPlay();
//        }
    }

    @Override
    public void onStart() {
        super.onStart();
//        if (banner != null) {
//            banner.startAutoPlay();
//        }

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        initData();
    }


}
